package br.com.me.ceap.up;

import br.com.me.ceap.BaseMojo;
import br.com.me.ceap.JarBundler;
import br.com.me.ceap.MECEAPUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import java.io.File;
import java.util.Date;

/**
 * Goal which uploads initial data xls to meceap.
 *
 * @goal uploadDataXLS
 * @requiresProject
 */

public class UploadDataXLSMojo extends BaseMojo {
// ------------------------------ FIELDS ------------------------------

    /**
     * Read Timeout for uploading Data XLS
     *
     * @parameter default-value="120000"
     */
    private long timeoutUploadDataXLS;

// ------------------------ INTERFACE METHODS ------------------------


// --------------------- Interface Mojo ---------------------

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        JarBundler jarBundler = bundleFilesInJAR("dataObject data in XLS", "src/main/data", new File(projectBaseDir, "src/main/data"), new IOFileFilter() {
            @Override
            public boolean accept(File file) {
                return true;
            }

            @Override
            public boolean accept(final File dir, final String name) {
                return name.endsWith(".data.xls");
            }
        },
        false);
        WebClient webClient = MECEAPUtils.forUploadDataFilesJAR(this, this.timeoutUploadDataXLS);

        this.getLog().debug("Timeout upload: " + this.timeoutUploadDataXLS);
        this.getLog().debug("Initiating upload: " + new Date());
        try {
            uploadFileToWebClient(webClient, jarBundler.toByteArray(), "Data in XLS");
        } finally {
            this.getLog().debug("Finalized upload: " + new Date());
        }
    }
}
